package main

type ListNode struct {
	Val  int
	Next *ListNode
}

/**
 *
 * @param head ListNode类
 * @param m int整型
 * @param n int整型
 * @return ListNode类
 */
func reverseBetween(head *ListNode, m int, n int) *ListNode {
	// write code here
	return solve(head, m, n, 1, nil, head)
}

func solve(head *ListNode, m int, n int, index int, pre *ListNode, h *ListNode) *ListNode {
	if index < m {
		head.Next = solve(head.Next, m, n, index+1, head, head.Next)
		return head
	}
	if index >= m && index <= n {
		node := head.Next
		head.Next = pre
		return solve(node, m, n, index+1, head, h)
	}
	h.Next = head
	return pre
}
